package sandbox

import akka.actor.Actor._
import akka.actor.{SupervisorFactory, Actor, Supervisor}
import akka.config.Supervision._

case object AreYouAlive
case object Kaboom

// Simple Actor that handles two types of messages: AreYouAlive and Kaboom
class SomeActor extends Actor {
  def receive = {
    case AreYouAlive => println("Yes I am alive!")
    case Kaboom => throw new RuntimeException("Kaboom!!!")
  }
}

object SomeApp extends App {
  val someActor = actorOf[SomeActor]

  // Setup the supervisorFactory with a config and a list of Actors to supervise
  val supervisorFactory = SupervisorFactory(
    SupervisorConfig(OneForOneStrategy(List(classOf[Exception]), 3, 1000),
    Supervise(someActor, Permanent) :: Nil)
  )
  // Instantiate and start the supervisor, this also starts all supervised Actors
  val supervisor = supervisorFactory.newInstance
  supervisor.start

  // Ping the Actor, yep it is working
  someActor ! AreYouAlive

  // Send the Kaboom message, oh no, the Actor will crash
  someActor ! Kaboom
  
  Thread.sleep(1000)

  // Wait, our Actor is alive and well again
  someActor ! AreYouAlive
  supervisor.shutdown()
}